package com.enation.app.shop.front.policyInsurance.adminController;

import com.enation.app.base.core.model.Member;
import com.enation.app.base.core.service.IMemberManager;
import com.enation.app.shop.core.goods.model.Cat;
import com.enation.app.shop.core.goods.model.Goods;
import com.enation.app.shop.core.goods.service.IGoodsCatManager;
import com.enation.app.shop.core.goods.service.IGoodsManager;
import com.enation.app.shop.front.api.utils.Json;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceArea;
import com.enation.app.shop.front.policyInsurance.entity.Supplier;
import com.enation.app.shop.front.policyInsurance.entity.SysArea;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiResponse;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiUtil;
import com.enation.app.shop.front.policyInsurance.service.ISupplierMapper;
import com.enation.app.shop.front.policyInsurance.service.ISysAreaMapper;
import com.enation.app.shop.front.policyInsurance.service.InsuranceAreaMapper;
import com.enation.eop.resource.IMenuManager;
import com.enation.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * web后台区域
 */
@Scope("prototype")
@Controller
@RequestMapping("/web/sysarea")
public class SysAreaController {
    @Autowired
    private ISysAreaMapper sysAreaMapper;
    @Autowired
    private IMemberManager memberManager;
    @Autowired
    private InsuranceAreaMapper insuranceAreaMapper;
    @Autowired
    private IGoodsManager goodsManager;
    @Autowired
    private ISupplierMapper supplierMapper;
    @Autowired
    private IMenuManager menuManager;
    @Autowired
    private IGoodsCatManager goodsCatManager;
    /**
     * 首先获取全部的省份
     // * @param map
     * @return
     */
    @RequestMapping(value="/staffsysarea")
    public ModelAndView allProvince(Model model){
        ModelAndView view = new ModelAndView();
        List<SysArea> sysAreas=sysAreaMapper.getByAreaLevel(1);
        model.addAttribute("provinceList",sysAreas);
        List<Member> members=memberManager.getByMemberType(2);
        model.addAttribute("member",members);
        view.setViewName("/shop/admin/admininsurance/addstaffarea");
        return view;
    }




    @RequestMapping(value="/insuran")
    public ModelAndView insurancearea(Model model){
        ModelAndView view = new ModelAndView();
        List<Cat> cats= goodsCatManager.listChildren(244);
        model.addAttribute("cat2Id",cats);
        List<SysArea> sysAreas=sysAreaMapper.getByAreaLevel(1);
        model.addAttribute("provinceList",sysAreas);
        List<Goods> goods=goodsManager.getByType(70);
        model.addAttribute("goods",goods);
        List<Member> members=memberManager.getByMemberType(2);
        model.addAttribute("member",members);
        List<Supplier> suppliers=supplierMapper.getAll();
        model.addAttribute("suppliers",suppliers);
    //    view.setViewName("/shop/admin/admininsurance/InsuranceArea");
        return view;
    }


    /**
     * 根据省份获取城市
     * @param id
     * @return
     */
    @RequestMapping("/getSysCityByProvinceId")
    @ResponseBody
    public Json getCityByProvinceId(String id){
        SysArea sysArea=sysAreaMapper.getByAreaCode(id);
        List<SysArea> sysAreas=sysAreaMapper.getByLevelAndPidAndCid(2,sysArea.getProvince_id(),null);
        if (sysAreas!=null){
            return new Json(true,"success",sysAreas);
        } else {
            return new Json(false,"fail",null);
        }
    }





    /**
     * 根据城市获取区域
     * @param id
     * @return
     */
    @RequestMapping("/getSysAreaByCityId")
    @ResponseBody
    public Json getAreaByCityId(String id){
        SysArea sysArea=sysAreaMapper.getByAreaCode(id);
        List<SysArea> sysAreas=sysAreaMapper.getByLevelAndPidAndCid(3,sysArea.getProvince_id(),sysArea.getCity_id());
        if (sysAreas!=null){
            return new Json(true,"success",sysAreas);
        } else {
            return new Json(false,"fail",null);
        }
    }


    /**
     * 根据区域获取街道
     * @param id
     * @return
     */
    @RequestMapping("/getSysStreetByAreaId")
    @ResponseBody
    public Json getStreetByArea(String id){
        SysArea sysArea=sysAreaMapper.getByAreaCode(id);
        List<SysArea> sysAreas=sysAreaMapper.getByLevelAndSidAndTid(4,sysArea.getProvince_id(),sysArea.getCity_id(),sysArea.getRegion_id(),null);
        if (sysAreas!=null){
            return new Json(true,"success",sysAreas);
        } else {
            return new Json(false,"fail",null);
        }
    }



    /**
     * 根据街道获取村
     * @param id
     * @return
     */
    @RequestMapping("/getSysVillageByStreetId")
    @ResponseBody
    public Json getVillagetByStreet(String id){
        SysArea sysArea=sysAreaMapper.getByAreaCode(id);
        List<SysArea> sysAreas=sysAreaMapper.getByLevelAndSidAndTid(5,sysArea.getProvince_id(),sysArea.getCity_id(),sysArea.getRegion_id(),sysArea.getStreet_id());
        if (sysAreas!=null){
            return new Json(true,"success",sysAreas);
        } else {
            return new Json(false,"fail",null);
        }
    }


    @ResponseBody
    @RequestMapping("/getCat3ByCat2")
    public Json getVillagetByStreet(Integer id){
        List<Cat> cats=goodsCatManager.listChildren(id);
        if (cats!=null){
            return new Json(true,"success",cats);
        } else {
            return new Json(false,"fail",null);
        }
    }

    /**
     * 添加政策性保险的区域及供应商
   //  * @param pid
     * @param cityId
     * @param areaId
     //* @param good
     * @param supplier
     * @return
     */
    @ResponseBody
    @RequestMapping("/addgood")
    public Boolean creatInsuranceArea( String provinceId,
                                       String cityId,
                                       String areaId,
                                       Integer cat3Id,
                                       String insurancename,
                                       String sn,
                                       double unit_money,
                                       double mtkprice,
                                       double max_money,
                                       Integer status,
                                       String pic,
                                       String item,
                                       String unit,
                                       double insured_subsidies,
                                       double central_subsidies,
                                       Integer province_subsidies,
                                       Integer city_subsidies,
                                       Integer region_subsidies,
                                       String other_subsidies,
                                       String insurance_accountfor,
                                       Integer descprtion,
                                       Integer  supplier){
        Goods goods=new Goods();
        SysArea area1=sysAreaMapper.getByAreaCode(provinceId);
        SysArea area2=sysAreaMapper.getByAreaCode(cityId);
        SysArea area3=sysAreaMapper.getByAreaCode(areaId);
        goods.setSupplier_id(supplier);
        if (areaId!=null){
            goods.setRegion(area3.getArea_name());
            goods.setRegion_id(area3.getRegion_id());
            goods.setArea_code(areaId);
        }else {
            if (cityId!=null){
                goods.setCity(area2.getArea_name());
                goods.setCity_id(area2.getCity_id());
                goods.setArea_code(cityId);
            }else {
                if (provinceId!=null){
                    goods.setProvince(area1.getArea_name());
                    goods.setProvince_id(area1.getProvince_id());
                    goods.setArea_code(provinceId);
                }
            }
        }
        goods.setCat_id(cat3Id);
        goods.setName(insurancename);
        goods.setSn(sn);
        goods.setMarket_enable(status);
        goods.setMktprice(mtkprice);
        goods.setPrice(unit_money);
        goods.setUnit(unit);
        goods.setThumbnail(pic);
        goods.setGoods_type("70");
        goods.setHave_spec(1);
        goods.setCreate_time( DateUtil.getDateline());
        goods.setLast_modify( DateUtil.getDateline());
        goods.setView_count(0);
        goods.setBuy_count(0);
        goods.setDisabled(0);
        goods.setStore(10000);
        goods.setEnable_store(10000);
        try {
            goodsManager.editapp(goods);
            return true;
        }catch (Exception e){

            return false;
        }
    }


    /**
     * web添加员工负责区域
     * @param pid
     * @param cityId
     * @param areaId
     * @param staff
     * @return
     */
    @ResponseBody
    @RequestMapping("/creatStaffArea")
    public Boolean creatStaffArea( String pid,
                                   String cityId,
                                   String areaId,
                                   String streetId,
                                   String villageId,
                                   Integer staff
    ){
        InsuranceArea insuranceArea=new InsuranceArea();
        Member member= memberManager.getapp(staff);
        SysArea area1=sysAreaMapper.getByAreaCode(pid);
        SysArea area2=sysAreaMapper.getByAreaCode(cityId);
        SysArea area3=sysAreaMapper.getByAreaCode(areaId);
        SysArea area4=sysAreaMapper.getByAreaCode(streetId);
        SysArea area5=sysAreaMapper.getByAreaCode(villageId);
        insuranceArea.setProvince(area1.getArea_name());
        insuranceArea.setProvince_id(area1.getProvince_id());
        insuranceArea.setCity(area2.getArea_name());
        insuranceArea.setCity_id(area2.getCity_id());
        insuranceArea.setRegion(area3.getArea_name());
        insuranceArea.setRegion_id(area3.getRegion_id());
        if (area4!=null){
            insuranceArea.setStreet_id(area4.getStreet_id());
            insuranceArea.setStreet(area4.getArea_name());
            if (area5!=null){
                insuranceArea.setVillage_id(area5.getVillage_id());
                insuranceArea.setVillage(area5.getArea_name());
                insuranceArea.setArea_code(area5.getArea_code());
            }else {
                insuranceArea.setArea_code(area4.getArea_code());
            }
        }else {
            insuranceArea.setArea_code(area3.getArea_code());
        }
        insuranceArea.setInsurance_staff_id(staff);
        insuranceArea.setInsurance_staff_name(member.getName());
        try {
            insuranceAreaMapper.insert(insuranceArea);
            return true;
        }catch (Exception e){
            return false;
        }
    }


    /**
     * web端保险区域列表显示
     * @return
     */
    @ResponseBody
    @RequestMapping("/getinsurancearealist")
    public LayuiUtil getAllTemplatetest(Integer page,Integer limit,String seacher){
        if (seacher==null){
            List<InsuranceArea> insuranceAreas=insuranceAreaMapper.getAll();
            List<InsuranceArea> insuranceAreaList= insuranceAreaMapper.getByPage((page-1)*limit,limit,seacher);
            return LayuiResponse.getObjectMessageJson(insuranceAreaList,"ss",insuranceAreas.size());
        }else {
            List<InsuranceArea> insuranceAreas=insuranceAreaMapper.getBySercher(seacher);
            List<InsuranceArea> insuranceAreaList= insuranceAreaMapper.getByPage((page-1)*limit,limit,seacher);
            return LayuiResponse.getObjectMessageJson(insuranceAreaList,"ss",insuranceAreas.size());
        }

    }




}
